package cn.iocoder.yudao.module.system.controller.admin.examtest;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.datapermission.core.annotation.DataPermission;
import cn.iocoder.yudao.module.system.controller.admin.examtest.vo.*;
import cn.iocoder.yudao.module.system.convert.examtest.ExamTestConvert;
import cn.iocoder.yudao.module.system.dal.dataobject.examtest.ExamTestDO;
import cn.iocoder.yudao.module.system.service.examtest.ExamTestService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Collection;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

@Tag(name = "管理后台 - 考试信息")
@RestController
@RequestMapping("/system/exam-test-info")
@Validated
public class ExamTestController {

    @Resource(name = "examTestManagementServiceImpl")
    private ExamTestService examTestService;

    @PostMapping("/create")
    @Operation(summary = "创建考试信息")
    @ApiAccessLog(operateType = CREATE)
    public CommonResult<Long> createExamTest(@Valid @RequestBody ExamTestCreateReqVO createReqVO) {
        return success(examTestService.createExamTest(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新考试信息")
    @ApiAccessLog(operateType = UPDATE)
    public CommonResult<Boolean> updateExamTest(@Valid @RequestBody ExamTestUpdateReqVO updateReqVO) {
        examTestService.updateExamTest(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除考试信息")
    @Parameter(name = "id", description = "编号", required = true)
    @ApiAccessLog(operateType = DELETE)
    public CommonResult<Boolean> deleteExamTest(@RequestParam("id") Long id) {
        examTestService.deleteExamTest(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得考试信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('system:exam-test:query')")
    @DataPermission
    public CommonResult<ExamTestRespVO> getExamTest(@RequestParam("id") Long id) {
        ExamTestDO examTest = examTestService.getExamTest(id);
        return success(ExamTestConvert.INSTANCE.convert(examTest));
    }

    @GetMapping("/list")
    @Operation(summary = "获得考试信息列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    public CommonResult<List<ExamTestRespVO>> getExamTestList(@RequestParam("ids") Collection<Long> ids) {
        List<ExamTestDO> list = examTestService.getExamTestList(ids);
        return success(ExamTestConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得考试信息分页")
    @DataPermission
    public CommonResult<PageResult<ExamTestRespVO>> getExamTestPage(@Valid ExamTestPageReqVO pageReqVO) {
        PageResult<ExamTestDO> pageResult = examTestService.getExamTestPage(pageReqVO);
        return success(ExamTestConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出考试信息 Excel")
    @PreAuthorize("@ss.hasPermission('system:exam-test:export')")
    @DataPermission
    @ApiAccessLog(operateType = EXPORT)
    public void exportExamTestExcel(@Valid ExamTestPageReqVO exportReqVO,
                                    HttpServletResponse response) throws IOException {
        List<ExamTestDO> list = examTestService.getExamTestList(exportReqVO);
        // 导出 Excel
        List<ExamTestRespVO> datas = ExamTestConvert.INSTANCE.convertList(list);
        ExcelUtils.write(response, "考试信息.xls", "数据", ExamTestRespVO.class, datas);
    }

    @GetMapping("/list-by-class")
    @Operation(summary = "根据班级ID获取考试列表")
    @Parameter(name = "classId", description = "班级ID", required = true, example = "1")
    public CommonResult<List<ExamTestRespVO>> getExamTestListByClassId(@RequestParam("classId") Long classId) {
        List<ExamTestDO> list = examTestService.getExamTestListByClassId(classId);
        return success(ExamTestConvert.INSTANCE.convertList(list));
    }



    @PutMapping("/update-status")
    @Operation(summary = "更新考试状态")
    public CommonResult<Boolean> updateExamTestStatus(@RequestParam("id") Long id,
                                                      @RequestParam("status") Integer status) {
        examTestService.updateExamTestStatus(id, status);
        return success(true);
    }

    @PutMapping("/start")
    @Operation(summary = "开始考试")
    @Parameter(name = "id", description = "考试ID", required = true)
    public CommonResult<Boolean> startExamTest(@RequestParam("id") Long id) {
        examTestService.startExamTest(id);
        return success(true);
    }

    @PutMapping("/end")
    @Operation(summary = "结束考试")
    @Parameter(name = "id", description = "考试ID", required = true)
    public CommonResult<Boolean> endExamTest(@RequestParam("id") Long id) {
        examTestService.endExamTest(id);
        return success(true);
    }

    @GetMapping("/tree")
    @Operation(summary = "获取考试信息树形结构（包含班级信息）")
    @Parameter(name = "id", description = "考试ID", required = true, example = "1")
    @DataPermission
    public CommonResult<ExamTestTreeRespVO> getExamTestTree(@RequestParam("id") Long id) {
        ExamTestTreeRespVO treeVO = examTestService.getExamTestTree(id);
        return success(treeVO);
    }

    @GetMapping("/tree-list")
    @Operation(summary = "获取考试信息树形结构列表（包含班级信息）")
    @DataPermission
    public CommonResult<List<ExamTestTreeRespVO>> getExamTestTreeList(@Valid ExamTestPageReqVO pageReqVO) {
        List<ExamTestTreeRespVO> treeList = examTestService.getExamTestTreeList(pageReqVO);
        return success(treeList);
    }

}